<?php	
	require_once('../includes/config.php');
	require_once('../includes/functions.php');

	// Retrieve base HTML
	$pageStr = buildStandardPage('Delete an Account', 'Delete Account');
	$content = '';
	
	if(true == isBankManager() || true == isCustomer()){
		if(true == isset($_GET[$ACCOUNT_ID])){
			
			$id = getID();
			$content .= makeAccountNavBar($id);
			
			// Make sure it is not the last account!!!
			$query = buildSelectStatement('account', array('userid' => $id));
			$result = executeSQL($query, 'Failed to delete the account.');
			$numAccounts = mysql_num_rows($result);

			if($numAccounts > 1){

				$query = buildDeleteStatement('account', $_GET[$ACCOUNT_ID]);
				$result = executeSQL($query, 'Failed to delete the account.');
				
				
				if(true == isCustomer()){ // Customers only have the option to delete inactive accounts, that do not have a balance.
					header('Location: ' . $ACCOUNTS_URL);
				}
				else{ // Remind the Bank Manager to withdraw or transfer the remaining balance.
					$content .= '<strong>Note: </strong>Be sure to deposit the remaining balance into another account, or withdraw the cash for the customer.';
				}
			}
			else{
				if(false == isCustomer()){ // This is the customer's last account, tell the bank manager to de-activate their account.
					$content .= 'This is the user\'s last account, they need to have at least one account to be a member. <br/><br/>'
					. 'Please, talk to the customer about their preference. '
					.'If the user would like to leave the bank, withdraw all of their money and de-activate their user profile.'
					. ' Don\'t worry about deleting the account.';
				}
				else{
					$content .= 'Sorry, you must have one account to bank with us. Please go to the nearest physical bank to close this account.';
				}
			}
		}
		else{
			if(true == isBankManager()){
				header('Location: ' . $USERS_URL);
			}
			else{
				header('Location: ' . $ACCOUNTS_URL);				
			}
		}
	}
	else{
		header('Location: ' . $SITE_URL);
	}
	
	$pageStr = str_Replace('<!--content-->', $content, $pageStr);
	echo $pageStr;
?>